package com.example.jpa_demo.controller;

import com.example.jpa_demo.entity.ClassInfo;
import com.example.jpa_demo.entity.Student;
import com.example.jpa_demo.repository.ClassInfoRepository;
import com.example.jpa_demo.repository.StudentRepository;
import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description
 * @Author lihw
 * @Date 2023/12/11 14:15
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private EntityManager entityManager;

    @Autowired
    private ClassInfoRepository classInfoRepository;

    @Autowired
    private StudentRepository studentRepository;

    @GetMapping("save")
    @Transactional
    public void save() {
        Student student = new Student();
        student.setName("李以勒");
        student.setAge(20);
        student.setClassId(2L);
        entityManager.persist(student);

    }

    @GetMapping("class_info")
    @Transactional
    public List<ClassInfo> queryClassInfo() {
        List<ClassInfo> all = classInfoRepository.findAll();
        return all;
    }

    @GetMapping("stu_list")
    @Transactional
    public List<Student> queryStudentList() {
        List<Student> all = studentRepository.findAll();
        return all;
    }
}
